package com.zaimokuza.cn.controller;

import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.message.AlertMessage;
import com.zaimokuza.cn.entity.message.Message;
import com.zaimokuza.cn.entity.message.vo.MessageInfo;
import com.zaimokuza.cn.entity.response.ResponseFormat;
import com.zaimokuza.cn.service.MessageService;
import com.zaimokuza.cn.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zaimokuza
 */
@Api(tags = "消息相关接口")
@ResponseFormat
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageService messageService;

    @ApiOperation(value = "发送消息", notes = "发送消息")
    @PostMapping("/send")
    public void send(@RequestBody Message message) {
        // 以当前用户作为发送人
        message.setSenderId(UserUtils.getUserId());
        // 发送信息
        messageService.send(message);
    }

    @ApiOperation(value = "举报用户", notes = "举报用户")
    @PostMapping("/alert")
    @Transactional(rollbackFor = Exception.class)
    public void alert(@RequestBody AlertMessage alertMessage) {
        // 以当前用户作为发送人
        alertMessage.setSenderId(UserUtils.getUserId());
        // 发送举报信息
        messageService.alert(alertMessage);
    }

    @ApiOperation(value = "处理用户举报", notes = "处理用户举报")
    @PutMapping("/alert/handle")
    @Transactional(rollbackFor = Exception.class)
    public void handleAlert(@RequestBody AlertMessage alertMessage) {
        // 以当前用户作为举报处理人
        alertMessage.setHandleUserId(UserUtils.getUserId());
        // 进行举报信息处理
        messageService.handleAlert(alertMessage);
    }

    @ApiOperation(value = "阅读消息", notes = "修改当前用户和指定联络人的所有接收消息状态")
    @PutMapping("/read/{friendId}")
    public void updateMessageStatus(@PathVariable @ApiParam("联络人ID") String friendId) {
        messageService.updateMessageStatus(UserUtils.getUserId(), friendId);
    }

    @ApiOperation(value = "查询分页的消息列表", notes = "根据目标用户查询分页的消息列表")
    @GetMapping("/page")
    public PageResult<MessageInfo> getMessagePage(@RequestParam @ApiParam("联络人ID") String friendId,
                                                  @RequestParam(defaultValue = "1") @ApiParam("当前页") Integer page,
                                                  @RequestParam(defaultValue = "10") @ApiParam("每页条数") Integer size) {
        return messageService.getMessagePage(UserUtils.getUserId(), friendId, page, size);
    }

    @ApiOperation(value = "查询分页的举报信息", notes = "查询分页的举报信息")
    @GetMapping("/alert/page")
    public PageResult<AlertMessage> getAlertMessagePage(@RequestParam(required = false) @ApiParam("是否处理") Boolean isHandle,
                                                        @RequestParam(defaultValue = "1") @ApiParam("当前页") Integer page,
                                                        @RequestParam(defaultValue = "10") @ApiParam("每页条数") Integer size) {
        return messageService.getAlterMessagePage(isHandle, page, size);
    }

}
